/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//layout_order.hpp

#ifndef MVL_LAYOUT_ORDER_HPP
#define MVL_LAYOUT_ORDER_HPP

#include "layout.hpp"
#include "layout_trans.hpp"

namespace mvl {

namespace layout {

//order entries
template<typename I, typename J, typename Tag, typename Src>
struct order_sparse_entry {
    typedef Tag                 tag;           //type of entry
    typedef I                   i;
    typedef J                   j;
    typedef Src                 src;            //refers to the entry in the source layout
    typedef order_sparse_entry  type;
};

//
struct new_order_entry {
    template<typename Entry>
    struct apply : order_sparse_entry<typename Entry::i, typename Entry::j, typename Entry::tag, Entry> { };
};

template<typename L, typename NewOrder>
struct layout_order 
{
    typedef L layout_t;
    typedef NewOrder new_order;
    typedef typename meta::is_same<typename layout_t::order, new_order>::type is_same_order;
    typedef typename meta::is_same<typename layout_t::tag, layout::dense_layout_tag>::type is_dense_layout;

    typedef typename meta::eval_if<
        is_same_order,
        meta::identity<layout_t>,
        meta::eval_if<
            is_dense_layout,
            layout::dense_layout<
                layout_t::Rows::value,
                layout_t::Cols::value,
                new_order
            >,
            layout::layout_sparse_trans<
                layout_t,
                new_order_entry,  
                typename layout_t::Rows,
                typename layout_t::Cols,
                new_order
            >
        >
    >::type type;
};

} //namespace layout

} //namespace mvl

#endif
